<template>
  <div class="app-container">
    <el-form ref="queryForm" :inline="true" label-width="120px">
      <el-form-item label="筛选时间">
        <el-date-picker
          v-model="dateRange"
          type="daterange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item label="订单类型" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="订单类型"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in typeList"
            :key="dict.name"
            :label="dict.name"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="自提点名称" prop="goodsName" v-if="queryParams.pointName && queryParams.pointId">
        <el-input
          v-model="queryParams.pointName"
          ref="input"
          clearable
          disabled
        />
      </el-form-item>
      <el-form-item label="消费总额" prop="goodsName">
        <el-input
          v-model="queryParams.sumPayPrice"
          ref="input"
          placeholder="请输入消费总额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="消费次数" prop="goodsName">
        <el-input
          v-model="queryParams.sumCount"
          ref="input"
          placeholder="请输入消费次数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <!-- 商家 -->
    <el-table
      v-loading="loading"
      :data="tableData"
      stripe
      border
      style="width: 100%"
      :default-sort="{ prop: 'saleNum', order: 'descending' }"
    >
      <el-table-column prop="userId" label="用户ID" width="120" sortable>
      </el-table-column>
      <el-table-column prop="phonenumber" label="手机号码" sortable>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { orderUserStatistics } from "@/api/system/statistics";
import { getToken } from "@/utils/auth";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Statistics",
  data() {
    let now = new Date()
    let endTime = now.getFullYear()+'-'+(now.getMonth() + 1)+'-'+now.getDate()
    now.setDate(now.getDate() - 7)
    let startTime = now.getFullYear()+'-'+(now.getMonth() + 1)+'-'+now.getDate()

    return {
      dateRange: [startTime, endTime],
      queryParams: {
        pointId: '',
        pointName: '',
        type: "",
        startTime: startTime,
        endTime: endTime,
        sumPayPrice: "0",
        sumCount: "0"
      },
      loading: false,
      typeList: [
        {
          value: '',
          name: '全部'
        },
        {
          value: 10,
          name: '普通订单'
        },
        {
          value: 20,
          name: '免费送'
        },
        {
          value: 30,
          name: '拼团订单'
        },
        {
          value: 40,
          name: '批发专区'
        },
        {
          value: 50,
          name: '捶一捶'
        }
      ],
      tableData: []
    };
  },
  created() {
    this.queryParams.pointId = this.$route.query.pointId || ''
    this.queryParams.pointName = this.$route.query.pointName || ''
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      let search = {
        startTime: "",
        endTime: ""
      };
      if (null != this.dateRange && "" != this.dateRange) {
        search.startTime = this.dateRange[0] ? this.dateRange[0] : undefined;
        search.endTime = this.dateRange[1] ? this.dateRange[1] : undefined;
      }
      this.queryParams = {
        ...this.queryParams,
        ...search,
      };
      orderUserStatistics(this.queryParams).then((res) => {
        this.loading = false;
        this.tableData = res.data;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.queryParams = {
        ...this.$options.data.queryParams
      }
      this.handleQuery();
    },
  },
};
</script>
